<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background-color: rgba(255,255,255,0)
            }
        </style>
    </head>
    <body>
        <footer class="aui-bar aui-bar-tab" style="background-color: rgba(255,255,255,0);padding-top: 0rem;">
            <div class="aui-bar-tab-item" tapmode style="background-color: rgba(255,255,255,1); width: 3rem;" onclick="toggleFav()">
                <img id="favIcon" class="aui-iconfont" style="display:none; width:1rem; height:1rem; margin:0 auto;" src="../../image/main_frm_2/fav.png"></img>
                <img id="favIcon_selected" class="aui-iconfont" style="display:none; width:1rem; height:1rem; margin:0 auto;" src="../../image/main_frm_2/fav_selected.png"></img>
                <div id="favLabel" style="display:none" class="aui-bar-tab-label aui-text-warning">
                    收藏
                </div>
            </div>
            <div class="aui-bar-tab-item aui-text-white" tapmode style="background-color: rgba(229,28,35,0.9); width: auto;color: #fff" onclick="registerLoan()">
                立即申请
            </div>
        </footer>
    </body>
    <script type="text/javascript" src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/APICloud-rest-SHA1.js" ></script>
    <script type="text/javascript">
        var client = new Resource("A6928874653494", "4B6C428C-24C2-F9E7-3175-F983922E8C8F");
        var userRecord = client.Factory("user");
        var selected = false;
        var favorites = [];
        Array.prototype.contains = function(obj) {
            var i = this.length;
            while (i--) {
                if (this[i] == obj) {
                    return true;
                }
            }
            return false;
        }
        var isLogin = false;
        function openWinFrame(winName, options) {
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 300;
            }
            api.openWin({
                name : winName,
                url : './' + winName + '.html',
                bounces : false,
                delay : delay,
                slidBackEnabled : true,
                vScrollBarEnabled : false,
                animation : {
                    type : "movein", //动画类型（详见动画类型常量）
                    subType : "from_left", //动画子类型（详见动画子类型常量）
                    duration : delay //动画过渡时间，默认300毫秒
                },
                pageParam : options,
            });
        }

        function userLogin() {
            openWinFrame('user_login_win', null);
        }

        function toggleFav() {
            if (isLogin == false) {
                userLogin();
                return;
            }
            if (selected) {
                api.showProgress({
                    title : '取消收藏中...',
                    modal : true
                });
                userRecord.save({
                    "_id" : $api.getStorage('userID')
                }, {
                    "$pull" : {
                        "favorites" : api.pageParam.loanId,
                    }
                }, function(ret, err) {
                    if (err) {
                        //处理错误 err
                        alert(JSON.stringify(err));
                    } else {
                        //处理数据 ret
                        //alert(JSON.stringify(ret));
                        selected = ret.favorites.contains(api.pageParam.loanId);
                        setFavIcon();
                        api.hideProgress();
                    }
                })
            } else {
                api.showProgress({
                    title : '添加收藏中...',
                    modal : true
                });
                userRecord.save({
                    "_id" : $api.getStorage('userID')
                }, {
                    "$push" : {
                        "favorites" : api.pageParam.loanId,
                    }
                }, function(ret, err) {
                    if (err) {
                        //处理错误 err
                        alert(JSON.stringify(err));
                    } else {
                        //处理数据 ret
                        //alert(JSON.stringify(ret));
                        selected = ret.favorites.contains(api.pageParam.loanId);
                        setFavIcon();
                        api.hideProgress();
                    }
                })
            }
        }

        function registerLoan() {
            if (isLogin == false) {
                userLogin();
                return;
            }
            openWinFrame('loan_register_win', api.pageParam);
        }

        function setFavIcon() {
            var favLabel = document.getElementById("favLabel");
            var favIcon = document.getElementById("favIcon");
            var favIcon_selected = document.getElementById("favIcon_selected");
            if (selected) {
                favIcon.style.display = "none";
                favIcon_selected.style.display = "block";
                favLabel.textContent = "已收藏";
            } else {
                favIcon.style.display = "block";
                favIcon_selected.style.display = "none";
                favLabel.textContent = "收藏";
            }
            favLabel.style.display = "block";
        }

        function initFavIcon() {
            var db = api.require('db');
            db.selectSql({
                name : 'loanMarketDB',
                sql : 'SELECT * FROM UserInfo'
            }, function(ret, err) {
                if (ret.status) {
                    //alert("select tbl: " + JSON.stringify(ret));
                    if (ret.data.length) {
                        //alert(JSON.stringify(ret));
                        isLogin = true;
                        userRecord.query({
                            filter : {
                                "limit" : 1,
                                "where" : {
                                    "id" : $api.getStorage('userID')
                                }
                            }
                        }, function(ret, err) {
                            if (err) {
                                //处理错误 err
                                alert(JSON.stringify(err));
                            } else {
                                //处理数据 ret
                                //alert(JSON.stringify(ret));
                                if (ret.length) {
                                    favorites = ret[0].favorites;
                                    //alert(api.pageParam.loanId);
                                    selected = favorites.contains(api.pageParam.loanId);
                                }
                                setFavIcon();
                            }
                        });
                    } else {
                        isLogin = false;
                        setFavIcon();
                    }
                } else {
                    alert(JSON.stringify(ret));
                }
            });
        }

        apiready = function() {
            api.parseTapmode();
            initFavIcon();
            api.addEventListener({
                name : 'ev_loanBtn_login'
            }, function(ret, err) {
                //alert('login!');
                initFavIcon();
            });
        };
    </script>
</html>